{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = r'./使用数据'       \n",
    "out_path  = r'./输出数据'\n",
    "name_list = os.listdir(file_path) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_df1 = {'plot':[],'树种':[],'数量':[]}\n",
    "\n",
    "for i in name_list:\n",
    "    file = os.path.join(file_path,i)\n",
    "    df = pd.read_csv(file,encoding='utf_8_sig')\n",
    "    tree_names = list(df['树种'].unique())\n",
    "    for j in tree_names:\n",
    "        num = len(df.loc[df['树种']==j])\n",
    "        dict_df1['plot'].append(i)\n",
    "        dict_df1['树种'].append(j)\n",
    "        dict_df1['数量'].append(num)\n",
    "out_df1 = pd.DataFrame(dict_df1)      \n",
    "dict_df = {'plot':[],'shannon':[],'pielou_shannon':[],'simpson':[],'pielou_simpson':[]}\n",
    "for i in out_df1['plot'].unique():\n",
    "    clip = out_df1.loc[out_df1['plot']==i]\n",
    "    tree_list = clip['树种'].unique()\n",
    "    dict_df['plot'].append(i)\n",
    "    \n",
    "    H = []\n",
    "    D = []\n",
    "    for l in tree_list:\n",
    "        N = np.sum(clip['数量'].values)\n",
    "        S = len(np.unique(clip['树种']))\n",
    "        n = np.sum(clip[clip['树种']==l]['数量'].values)\n",
    "        pi = n/N       \n",
    "        h = pi*np.log(pi)\n",
    "        H.append(h)\n",
    "        d = pi*pi\n",
    "        D.append(d)\n",
    "    shannon = -np.sum(H)\n",
    "    simpson = 1-np.sum(D)\n",
    "    pieloush = shannon/np.log(S)\n",
    "    pielousi = simpson/(1-1/S)\n",
    "    dict_df['shannon'].append(shannon)\n",
    "    dict_df['simpson'].append(simpson)\n",
    "    dict_df['pielou_shannon'].append(pieloush)  \n",
    "    dict_df['pielou_simpson'].append(pielousi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>plot</th>\n",
       "      <th>shannon</th>\n",
       "      <th>pielou_shannon</th>\n",
       "      <th>simpson</th>\n",
       "      <th>pielou_simpson</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>样地数据.csv</td>\n",
       "      <td>1.332179</td>\n",
       "      <td>0.960964</td>\n",
       "      <td>0.72</td>\n",
       "      <td>0.96</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       plot   shannon  pielou_shannon  simpson  pielou_simpson\n",
       "0  样地数据.csv  1.332179        0.960964     0.72            0.96"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame(dict_df)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
